<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>

        <el-form-item label="时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户UID" align="center" width="120" />
          <el-table-column prop="assets_type" label="资产类型" align="center" />
          <el-table-column prop="amount" label="变动数量" align="center"  />
          <el-table-column prop="amount_before" label="变动前数量" align="center" width="150" />
          <el-table-column prop="amount_after" label="变动后数量" align="center" width="150" />
          <el-table-column prop="amount_type" label="变动类型" align="center" width="180" />

          <el-table-column prop="amount_child_type" label="子类型" align="center" width="200" />
          <el-table-column prop="source_id" label="来源ID" align="center" />
          <el-table-column prop="description" label="备注" align="center" width="180" />
          <el-table-column prop="app_mark" label="APP标识" align="center" />
          <el-table-column
            prop="createtime"
            label="变动时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";

import {
  formatterTime,
  formatter,
  exportToExcel,
  timestamp, pickerDefaultTime
} from "@/utils/utils";
import { assetsRecordListApi } from "@/api/slotsgame/assets/assets";

const tableData = ref([]);

const formData = {
  uid: "",
  timeData: []
};
const form = reactive({ ...formData });
const loading = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,

    uid: form.uid,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : ""
  };
};
//格式化

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await assetsRecordListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// const getTbaleData = async () => {
//   loading.value = true;

//   try {
//     const res = await assetsRecordListApi(param());
//     loading.value = false;
//     tableData.value = res.data.results;
//     pageable.total = res.data.count;
//   } catch (error) {
//     loading.value = false;
//   }
// };
// 重置
const onReset = () => {
  Object.assign(form, formData);
  pageable.pageNum = 1;
  getTbaleData();
};
// const onReset = () => {
//   Object.assign(form, formData);
// };
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await assetsRecordListApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    assets_type: "资产类型",
    amount: "变动数量",
    amount_before: "变动前数量",
    amount_after: "变动后数量",
    amount_type: "变动类型",
    amount_child_type: "子类型",
    source_id: "来源ID",
    description: "备注",
    app_mark: "APP标识",
    createtime: "变动时间"
  };
  exportToExcel(res.data.results, head, "Slotsgame-钻石资产记录");
};

// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = number => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
